package code.tools.commponents
{
	import code.structure.manager.font.FontManager;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.MovieClip;
	import flash.text.TextField;
	import flash.text.TextFormat;

	/**
	 *一个图标 + 一个文本
	 * 无交互事件 ,横向排列
	 * @author liangqian
	 * 
	 */	
	public class IconLabel extends Component
	{
		private var iconBm:MovieClip;
		private var tf:TextField;
		private var textFormat:TextFormat;
		private var _tfYpos:Number;
		private var _spacing:int = 5;
		/**
		 * 
		 * @param parent
		 * @param xpos
		 * @param ypos
		 * @param tfYpos 设置文本框在垂直居中的基础上的偏移量
		 * 
		 */		
		public function IconLabel(parent:DisplayObjectContainer, xpos:Number, ypos:Number,tfYpos:Number = -1)
		{
			_tfYpos = tfYpos;
			super(parent, xpos, ypos);
		
			textFormat = new TextFormat();
		
			tf = Helpers.FormatTextField(this);
			tf.mouseEnabled = false;
			tf.embedFonts = true;
		}
		
		public function setText(t:String,size:int = 12,color:uint = 0x999999,_filters:Array = null,fontName:String = null,spacing:int = 5):void
		{
			_spacing = spacing;
			textFormat.size = size;
			textFormat.color = color;
			textFormat.font = fontName == null ? FontManager.getInstance().embeddedFontBName : fontName;
			tf.defaultTextFormat = textFormat;
			tf.x = iconBm.width + spacing;
			tf.text = t;
			tf.filters = _filters;
			drawSize();
		}
		
		public function set icon(bmd:MovieClip):void
		{
			iconBm = bmd;
			addChild(iconBm);
			tf.x = iconBm.width + _spacing;
			drawSize();
		}
		
		private function drawSize():void
		{
			if(iconBm)
				setSize(Math.max(30,tf.width) + iconBm.width + _spacing,iconBm.height > tf.height ? iconBm.height : tf.height);
			else
				setSize(tf.width,tf.height);
			tf.y = (_height - tf.textHeight)*.5 + _tfYpos;
			iconBm.y = (_height - iconBm.height)*.5;
		}
		
	}
}